package example.exam.Shopee;

public class Demo2 {

    public static String Paired69(String S) {
        if (S.length() == 0) {
            return "";
        }
        if (S.length() == 1) {
            return "69";
        }
        char[] chars = S.toCharArray();
        int left = 0, right = chars.length - 1;
        char[] newChars = new char[S.length() * 2];
        int newLeft = 0, newRight = newChars.length - 1;
//        int newLeft = 0, newRight = chars.length;
        while (left <= right) {
            if (left == right) {
                if (chars[left] == '6') {
                    newChars[newLeft] = chars[left];
                    newChars[newLeft + 1] = '9';
                } else {
                    newChars[newLeft + 1] = chars[left];
                    newChars[newLeft] = '6';
                }
                break;
            }
            if (chars[left] != chars[right]) {
                newChars[newLeft++] = chars[left];
                newChars[newRight--] = chars[right];
            } else {
                if (chars[left] == '6') {
                    newChars[newLeft++] = chars[left];
                    newChars[newLeft++] = '9';
                    newChars[newRight--] = '9';
                    newChars[newRight--] = chars[left];
                } else {
                    newChars[newLeft++] = '6';
                    newChars[newLeft++] = chars[left];
                    newChars[newRight--] = chars[left];
                    newChars[newRight--] = '6';
                }
            }
            left++;
            right--;
        }
        int count = 0;
        for (int i = 0; i < chars.length-1; i += 2) {
            if (chars[i] != chars[i + 1]) {
                count++;
            }
        }
        if (count == 0) {
            return new String(newChars);
        } else {

            return new String(newChars).substring(count * 2).trim();
        }
//        return new String(newChars);
    }

    public static void main(String[] args) {
//        System.out.println(Paired69("66"));
        System.out.println(Paired69("666"));
        System.out.println(Paired69("6666666"));
    }
}
